/**
 * 
 */
package com.gmail.blackdog1987.carcare.activity;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.BaseActivity;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.adapter.ProductAdapter;
import com.gmail.blackdog1987.carcare.entity.TProduct;
import com.gmail.blackdog1987.carcare.entity.base.DataPage;
import com.gmail.blackdog1987.carcare.network.request.builder.ProductRequestBuilder;
import com.gmail.blackdog1987.carcare.network.response.IResponse;
import com.gmail.blackdog1987.carcare.network.response.ProductResponse;
import com.gmail.blackdog1987.carcare.network.response.handler.IResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.OnResponseListener;
import com.gmail.blackdog1987.carcare.network.response.handler.ProductResponseHandler;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

/**
 * @author wdy
 *
 */
public class ProductActivity extends BaseActivity implements OnClickListener, OnResponseListener, OnLastItemVisibleListener,
		OnRefreshListener2<ListView> {
	private Context mContext;
	ProductRequestBuilder mReq = new ProductRequestBuilder();
	ProductResponseHandler mHandler = new ProductResponseHandler();

	PullToRefreshListView list;
	ProductAdapter adapter;

	List<TProduct> products;

	DataPage<TProduct> page;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_product);
		mContext = this;
		initViews();
		reg();
		initData();
	}

	@Override
	public void initViews() {
		super.initViews();
	}

	@Override
	public void reg() {
		super.reg();
		findViewById(R.id.back).setOnClickListener(this);
	}

	public void initData() {
		list = (PullToRefreshListView) findViewById(R.id.product_list);
		list.setMode(Mode.BOTH);
		list.setOnRefreshListener(this);
		list.setOnLastItemVisibleListener(this);
		adapter = new ProductAdapter(this);
		list.setAdapter(adapter);
		mHandler.setOnResponseListener(this);
		mReq.setPageIndex(1).setPageSize(20).postJsonRequest(this, mHandler);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		}
	}

	@Override
	public void onResponse(IResponseHandler classz) {
		IResponse res = classz.getResponse();
		if (res instanceof ProductResponse) {
			page = ((ProductResponse) classz.getResponse()).datas;
			if (page.currentPageNo <= 1) {
				adapter.setDatas(page.datas);
			} else {
				adapter.addDatas(page.datas);
				adapter.notifyDataSetChanged();
			}
			list.onRefreshComplete();
		}
	}

	@Override
	public void onError(IResponseHandler classz) {
		Toast.makeText(mContext, "加载失败", Toast.LENGTH_LONG).show();
		list.onRefreshComplete();
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		mReq.setPageIndex(1).setPageSize(20).postJsonRequest(mContext, mHandler);
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		if (page == null) {
			mReq.setPageIndex(1).setPageSize(20).postJsonRequest(mContext, mHandler);
		} else if (page.currentPageNo >= page.lastPageNo) {
			Toast.makeText(mContext, "没有了", Toast.LENGTH_LONG).show();
			list.onRefreshComplete();
		} else {
			mReq.setPageIndex(page.nextPage).setPageSize(page.pageSize).postJsonRequest(mContext, mHandler);
		}
	}

	@Override
	public void onLastItemVisible() {
	}

}
